package com.bifang.core.dict.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import com.bifang.common.constant.ConstantValue;
import com.bifang.common.vo.UserVO;
import com.bifang.core.dao.model.DictSysUser;
import java.nio.charset.Charset;
import org.apache.commons.lang3.StringUtils;

public class UserSecureUtil {

    public static String aesEncrypt(String str) {
        return StringUtils.isBlank(str)
                ? str
                : SecureUtil.aes(Base64.decode(ConstantValue.default_aes_key))
                        .encryptBase64(str, Charset.forName("UTF-8"));
    }

    public static String aesDecrypt(String str) {
        try {
            return StringUtils.isBlank(str)
                    ? str
                    : SecureUtil.aes(Base64.decode(ConstantValue.default_aes_key))
                            .decryptStr(str, Charset.forName("UTF-8"));
        } catch (Exception e) {
            return str;
        }
    }

    public static String enSysUser(DictSysUser user) {
        String userId = "";
        String userName = "";
        String userPassword = "";
        String email = "";
        String cellphone = "";
        String status = "";
        String userType = "";
        String fullName = "";
        String idcard = "";
        if (user.getUserId() != null) {
            userId = user.getUserId();
        }
        if (user.getUserName() != null) {
            userName = user.getUserName();
        }
        if (user.getUserPassword() != null) {
            userPassword = user.getUserPassword();
        }
        if (user.getEmail() != null) {
            email = user.getEmail();
        }
        if (user.getCellphone() != null) {
            cellphone = user.getCellphone();
        }
        if (user.getStatus() != null) {
            status = user.getStatus();
        }
        if (user.getUserType() != null) {
            userType = user.getUserType();
        }
        if (user.getFullName() != null) {
            fullName = user.getFullName();
        }
        if (user.getIdcard() != null) {
            idcard = user.getIdcard();
        }

        String encodeRules =
                userId
                        + "~|~"
                        + userName
                        + "~|~"
                        + userPassword
                        + "~|~"
                        + email
                        + "~|~"
                        + cellphone
                        + "~|~"
                        + status
                        + "~|~"
                        + userType
                        + "~|~"
                        + fullName
                        + "~|~"
                        + idcard;
        String hashcode = aesEncrypt(encodeRules);
        return hashcode;
    }

    public static DictSysUser deSysUser(String str) {
        DictSysUser sysUser = new DictSysUser();
        String deStr = aesDecrypt(str);
        String[] strList = StringUtils.split(deStr, "~|~");
        if (strList != null) {
            sysUser.setUserId(strList[0]);
            sysUser.setUserName(strList[1]);
            sysUser.setUserPassword(strList[2]);
            sysUser.setEmail(strList[3]);
            sysUser.setCellphone(strList[4]);
            sysUser.setStatus(strList[5]);
            sysUser.setUserType(strList[6]);
            sysUser.setFullName(strList[7]);
            sysUser.setIdcard(strList[8]);
        }
        return sysUser;
    }
    /** 用户加密字段解密 */
    public static void aesDecryptUser(UserVO userVO) {
        // 手机号码解密
        if (StringUtils.isNotBlank(userVO.getCellphone())) {
            userVO.setCellphone(aesDecrypt(userVO.getCellphone()));
        }
        // 邮箱解密
        if (StringUtils.isNotBlank(userVO.getEmail())) {
            userVO.setEmail(aesDecrypt(userVO.getEmail()));
        }
        // 身份证解密
        if (StringUtils.isNotBlank(userVO.getIdcard())) {
            userVO.setIdcard(aesDecrypt(userVO.getIdcard()));
        }
    }
    /** password 生成工具类 */
    public static String createPassword(String userId, String password) {
        return SecureUtil.md5(userId + password);
    }
}
